<!DOCTYPE HTML>
<html lang="cn" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <%include("/common/header.html",{title:''}){}%>
    <title>施工团队</title>
    <style>
    </style>
</head>
<script type="text/javascript" src="//api.map.baidu.com/api?v=2.0&ak=IENn7ABARjQIkTeAKyh80VTkg2jYXf3K"></script>
<body>

<div class="x-nav" style="margin: 20px auto auto 15px;">
      <span class="layui-breadcrumb" lay-filter="breadcrumb" style="visibility: visible;">
        <a href="javascript:">管理系统</a><span lay-separator="">/</span>
        <a><cite>设备管理</cite></a>
      </span>
    <a class="layui-btn layui-btn-primary layui-btn-small" style="margin: -5px 14px 0px 0px;float:right;"
       href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon layui-icon-refresh-3"></i>
    </a>
</div>

<div class="x-body" style="margin: 20px auto auto 15px;">
    <div class="layui-row">
        <div class="layui-form layui-col-md12 x-so">
            <button class="layui-btn layui-btn-warm">已有设备数量:0个</button>
            <button class="layui-btn">已采集数据数量:0条</button>
        </div>
    </div>


    <div class="layui-table-main layui-form" style="height: 100%">
        <table id="myTableData" lay-filter="myTableData"  class="layui-hide"></table>
        <div id="test1"></div>
    </div>
</div>
<div class="layui-fluid">
    <div class="layui-card">
        <table id="LAY-user-manage" lay-filter="LAY-user-manage"></table>
        <script type="text/html" id="toolbarDemo">
            <button class="layui-btn layui-btn layui-btn-sm"  lay-event="add"><i class="layui-icon layui-icon-add-1"></i>新增设备</button>
        </script>
        <script type="text/html" id="table-useradmin-webuser">
            <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>修改</a>
            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
        </script>
    </div>
</div>

<!--弹出新增设备页面-->
<div class="layui-form" lay-filter="layuiadmin-form-useradmin"
     id="layuiadmin-form-useradmin"
     style="padding: 15px;display: none">
    <form class="layui-form" action="" lay-filter="component-form-element" id="myform">
        <input type="hidden" name="id">
        <div class="layui-form-item">
            <label class="layui-form-label">角色选择</label>
            <div class="layui-input-inline">
                <select lay-verify="required" lay-filter="roleName" name="authorId" id="roleId">
                    <option value="">请选择</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">*账号</label>
            <div class="layui-input-inline">
                <input type="text" name="phone" lay-verify="required" placeholder="请输入账号" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">*密码</label>
            <div class="layui-input-inline">
                <input type="text" name="passWord" lay-verify="required" placeholder="请输入密码" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">*电话</label>
            <div class="layui-input-inline">
                <input type="text" name="telePhone" lay-verify="required" placeholder="请输入电话" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">*真实姓名</label>
            <div class="layui-input-inline">
                <input type="text" name="name" lay-verify="required" placeholder="请输入真实姓名" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">*描述信息</label>
            <div class="layui-input-block">
                <textarea placeholder="请输入描述信息" class="layui-textarea" name="remker"></textarea>
            </div>
        </div>

        <div class="layui-form-item layui-form-text" id="createTime">
            <label class="layui-form-label">创建时间</label>
            <div class="layui-input-block">
                <input type="text" name="createDate" disabled placeholder="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" lay-filter="component-form-element-a">立即提交</button>
            </div>
        </div>
    </form>
</div>

<%include("/common/foot.html"){}%>
<!-- 角色选择下拉框 -->
<script id="demo2" type="text/html">
    {{#  layui.each(d.list2, function(index, item2){ }}
    <option {{d.aid== item2.id?"selected":""}} value="{{ item2.id }}">{{ item2.name}}</option>
    {{#  }); }}
    {{#  if(d.list2.length === 0){ }}
    <option value="">无数据</option>
    {{#  } }}
</script>
<script>
    layui.use(['index', 'table', 'jquery','form', 'laytpl','laydate', 'layedit', 'AjaxCommon', 'AliangLayui', 'AliangCommon'], function () {
        var $ = layui.$;
        var table = layui.table;
        var form = layui.form;
        var laydate = layui.laydate;
        var AjaxCommon = layui.AjaxCommon;
        var AliangLayui = layui.AliangLayui;
        var laytpl=layui.laytpl;

        var title = "新增设备";
        var findAllAccount_url="system/openAccount/findAll.jhtml";
        var openAccount_url="system/openAccount/openAccount.jhtml";
        var delete_url="system/openAccount/deleteById.jhtml";
        var authority_url="system/openAccount/findAuthority.jhtml";
        var index;//定义建立富文本编辑器全局变量
        var layedit;//定义富文本编辑器layedit全局变量
        var cols = [ //表头
             {field:'id1',sort:'true',title:'已有设备(编号)',align:'center'},
            {
                field:'name1',sort:'true',title:'传感器类型',align:'center'
            },
            {
                field:'phone1',sort:'true',title:'安装位置',align:'center'
            },
            // {title: '权限等级',align:'center',templet:"<div>{{d.authority.name}}</div>"},
            {field: 'createDate1',sort:'true', title: '状态(运行/维修/损坏)',align:'center'},
            {title: '操作', fixed: 'right', width: 215, toolbar: '#table-useradmin-webuser',align:'center'}

        ];
        AliangLayui.createTable(
            "#LAY-user-manage",
            "listReload",
            "#toolbarDemo",
            title,
            findAllAccount_url,
            true,
            [10, 15, 20, 25],
            ['filter', 'print', 'exports'],
            cols,
            null
        );
        var findInfo_url="system/openAccount/findAll.jhtml";
        var keyWord;
        $('#searchBtn').on('click', function () {
            keyWord = $('#search_term').val();
            if (""==keyWord||undefined==keyWord){
                window.location.reload();
            }else{
                table.reload('listReload', {
                    url: findInfo_url,
                    where: {
                        keyWord: keyWord
                    },
                    page: {
                        curr: 1//重新从第 1 页开始
                    }
                });
            }
        });
        //////////////////////////////////////以上是修改每个页面修改的地方
        //弹出修改页面调用初始化数据方法
        var title1="开设账号";
        var size = ['800px', '600px'];
        function initUpdate(data) {
            layer.open({
                type: 1,
                title: title1,
                area:size,
                content: $('#layuiadmin-form-useradmin'),
                cancel: function (index, layero) {//取消事件
                    location.replace(location.href);　//layer.open关闭刷新
                },
                success: function (layero, index) {
                    layer.iframeAuto(index);
                }
            });
            console.log(data.authority.id)
            var aid=data.authority.id;
            $.ajax({
                url: authority_url,
                type: "post",
                data:{
                },
                dataType: "json",
                success: function(ret){

                    var laytpl = layui.laytpl;
                    var data = { //数据
                        "list2": ret.obj,
                        aid:aid
                    };
                    laytpl($("#demo2").html()).render(data, function (html) {
                        $("#roleId").html(html);
                    });
                    form.render();
                },
                error:function(jqXHR, textStatus, errorThrown){

                }
            });
            $("#createTime").hide();
            if (data==null){
                $('#myform')[0].reset();

            }else{
                layui.form.val("component-form-element", loadData(data));
            }
            //富文本表单验证
            form.verify({
                introduce: function (value) {
                    var val = layedit.getText(index);
                    if (val.length < 6) {
                        return '内容至少6个字符'
                    }
                }
            });
        }


        function loadData(data){
            var ret = {
                "id": data.id,
                "phone": data.phone,
                "passWord": data.passWord,
                "telePhone": data.telePhone,
                "name": data.name,
                "remker": data.remker,
                // "role_name":data.sysRole.sysRoleCode.role_name,
                // "roleId":data.sysRole.id,
                // "companyName":data.companyName
                "createDate":layui.util.toDateString(data.createDate)
            };
            $("#createTime").show();
            form.render();
            return ret;
        }


        //验证通过后提交数据到后台
        form.on('submit(component-form-element)', function(data){
            openAccount(data);
            return false;
        });

        //头工具栏事件
        table.on('toolbar(LAY-user-manage)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'add':
                    initUpdate();
                    break;
                case 'delete':
                    var data = checkStatus.data;
                    if (data.length > 0) {
                        //var id = new Array(); //声明数组
                        for (var i in data) {
                            var a = data[i].id;//得到复选框的值
                            postDelete(a);
                        }
                    } else {
                        layer.msg("至少选择一个进行删除");
                    }
                    break;
                case 'export':
                    layer.msg("导出");
                    break;
            }
        });

        //点击每行右工具栏“操作”里面按钮效果
        table.on('tool(LAY-user-manage)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                initUpdate(data);
            } else if (obj.event === 'view') {
            }else if (obj.event === 'del') {
                layer.confirm('删除以后数据将不能恢复，确定删除吗？', function(index) {
                    postDelete(data.id);
                });
            }
        });
        //表单回显，赋值
        window.postDelete = function (id) {
            $.ajax({
                url:delete_url,
                type: "POST",
                data: {
                    "pid": id
                },
                success: function (ret) {
                    if (ret == null) {
                        layer.msg("删除失败", {
                            icon: 5
                        });
                    } else {
                        layer.msg("删除成功", {
                            icon: 6
                        });
                        layer.closeAll();
                        table.reload('listReload');
                    }
                    return ret;
                }
            })
        };

        //开设账户ajax
        window.openAccount = function (data) {
            var formData=new FormData();
            formData.append("data",JSON.stringify(data.field));
            $.ajax({
                url: openAccount_url,
                type: "POST",
                data: formData,
                processData: false,
                contentType: false,
                success: function (data) {
                    if (data.code == 0) {
                        layer.closeAll();
                        layer.msg(data.msg, {time: 600, icon: 1}, function () {
                            table.reload('listReload');
                        });
                        location.replace(location.href);
                    } else {
                        layer.msg(data.msg, {icon: 6});
                    }
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    var estr = "Page:" + window.location.pathname + ",Function:postUpdate()";
                    layer.open({
                        title: "错误信息",
                        type: 2,
                        content: "base/error.jhtml?info=" + estr,
                        btn: ['关闭'] //可以无限个按钮
                    });
                }
            });
        };

    });

</script>
</body>
</html>
